import java.util.Comparator;
import java.util.PriorityQueue;
class Person implements Comparable<Person>{
    public int age;
    public String name;

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }
    @Override
    public String toString() {
        return "CompareTo{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
    //重写方法,比较年龄
    @Override
    public int compareTo(Person o) {
        return this.age - o.age;
    }
}
class NameComparator implements Comparator<Person> {
    public int compare(Person o1,Person o2) {
        return o1.compareTo(o2);
    }
}
class Imp implements Comparator<Integer> {
    public int compare(Integer o1,Integer o2) {
        return o2.compareTo(o1);
    }
}

public class Test {
    public int[] smallestK1(int[] arr,int k) {
        //直接利用优先级队列的默认排序方式为小根堆,直接返回k个最小值,
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        //1.插入元素
        for(int i = 0;i < arr.length;i++) {
            priorityQueue.offer(arr[i]);
        }
        //2.出元素
        int[] tmp = new int[k];
        for(int i = 0;i < k;i++) {
            tmp[i] = priorityQueue.poll();
        }
        //3.返回数组
        return tmp;
    }
    public static void main(String[] args) {
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        TestHeap testHeap = new TestHeap();
        testHeap.setElem(array);
        testHeap.createBigHeap();
        testHeap.heapSort();
        System.out.println("=========");
    }
    public static void main3(String[] args) {
        //转换大小堆的两种方式
        //1.实现comparator接口(重写)
        //2.使用比较器
        Imp imp = new Imp();
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(imp);
        priorityQueue.offer(10);
        priorityQueue.offer(4);
        int num1 = priorityQueue.poll();
        int num2 = priorityQueue.poll();
        System.out.println("ret1 = " + num1 + " ret2 = " + num2);
    }
    public static void main2(String[] args) {
        //优先级队列注意事项,需要进行导包
        //1.泛型必须是能够进行比较大小的,否则无法进行排序
        //2.实例化数据元素不能未空,否则会报空异常错误
        //3.没有容量限制,可以直接插入,内部自动进行扩容
        //4.插入删除时间复杂度为log2n
        //5.priorityQueue底层使用堆进行实现
        //6.底层排序默认是小根堆->每次获取元素会获得最小的元素
        PriorityQueue<Integer> priorityQueue  = new PriorityQueue<>();
        priorityQueue.offer(10);
        priorityQueue.offer(4);
        int num1 = priorityQueue.poll();
        int num2 = priorityQueue.poll();
        System.out.println("ret1 = " + num1 + " ret2 = " + num2);
    }
    public static void main1(String[] args) {
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        TestHeap testHeap = new TestHeap();
        testHeap.setElem(array);
        testHeap.createBigHeap();
        System.out.println("-----------");
//        testHeap.poll();
//        System.out.println("------------");
        testHeap.offer(80);
        System.out.println("+++++++");
    }
}
